package com.tian.hello.ui

import android.Manifest
import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ObjectAnimator
import android.content.Context
import android.content.Intent
import android.graphics.drawable.AnimationDrawable
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Parcelable
import android.provider.MediaStore
import android.util.Log
import android.view.View
import android.view.animation.*
import android.widget.TextView
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContract
import com.tian.arch.function.permission.PermissionManager
import com.tian.hello.databinding.ActivityAnimationTestBinding

/**
 * 动画的使用复习
 */
class AnimationTestActivity : AppCompatActivity() {

    private lateinit var binding: ActivityAnimationTestBinding


    private val acResultContract = object : ActivityResultContract<Intent, Intent>() {
        override fun createIntent(context: Context, input: Intent): Intent {
            return input
        }

        override fun parseResult(resultCode: Int, intent: Intent?): Intent {
            return intent!!
        }
    }


    private val resultLauncher = registerForActivityResult(acResultContract) {
        Log.i("tt", "-- $it")
    }

    private fun createChooserIntent(): Intent {
        val carmeraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE) // 相机intent

        val contentIntent = Intent(Intent.ACTION_GET_CONTENT) // 检索文件intent
        contentIntent.setType("*/*")
        val galleryIntent = Intent(Intent.ACTION_PICK)  //  相册intent

        galleryIntent.setType("image/*")
        val chooserIntent = Intent(Intent.ACTION_CHOOSER)
        chooserIntent.putExtra(Intent.EXTRA_TITLE, "选择图片来源")
        chooserIntent.putExtra(Intent.EXTRA_INTENT, galleryIntent) //这个参数不理解解，把galleryIntent换成sendIntent效果会不同。理解是干啥的留个言哈，感谢(*╹▽╹*)
        chooserIntent.putExtra(
            Intent.EXTRA_INITIAL_INTENTS,
            arrayOf<Parcelable>(
                carmeraIntent,
                contentIntent,
            )
        )

        return chooserIntent
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityAnimationTestBinding.inflate(layoutInflater)
        setContentView(binding.root)


        binding.pTestBtn.setOnClickListener {
            PermissionManager.with(this@AnimationTestActivity)
                .permissions(Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION)
                .onGranted {
                    resultLauncher.launch(createChooserIntent())
                }
                .start()

        }

        binding.testFrameAnimationBtn.setOnClickListener {
            val frameAnimation: AnimationDrawable = binding.frameImg.drawable as AnimationDrawable

            if (frameAnimation.isRunning) {
                frameAnimation.stop()
            } else {
                frameAnimation.start()
            }
        }
        binding.alphaBtn.setOnClickListener {
            toast("动画开始之前 img.alpha = " + binding.tweenAnimationImg.alpha)
            val alphaAnimation = AlphaAnimation(1.0f, 0.1f)
            alphaAnimation.duration = 1000
            // 设置插值器 为线性匀速改变
            alphaAnimation.interpolator = LinearInterpolator()
            // 动画执行完之后是否保留动画结束时的状 true 保存，false 不保存（默认 false）
            alphaAnimation.fillAfter = true
            alphaAnimation.setAnimationListener(object : Animation.AnimationListener {
                override fun onAnimationStart(animation: Animation?) {
                }

                override fun onAnimationEnd(animation: Animation?) {
                    toast("动画结束之后 img.alpha = " + binding.tweenAnimationImg.alpha)
                }

                override fun onAnimationRepeat(animation: Animation?) {
                }

            })

            binding.tweenAnimationImg.startAnimation(alphaAnimation)
        }
        binding.scaleBtn.setOnClickListener {
            toast("动画开始之前 img.scale = " + binding.tweenAnimationImg.scaleX + "," + binding.tweenAnimationImg.scaleY)
            val scaleAnimation = ScaleAnimation(1.0f, 2f, 1f, 2f)
            scaleAnimation.interpolator = AccelerateInterpolator()
            scaleAnimation.duration = 800
            scaleAnimation.setAnimationListener(object : Animation.AnimationListener {
                override fun onAnimationStart(animation: Animation?) {
                }

                override fun onAnimationEnd(animation: Animation?) {
                    toast("动画开始之前 img.scale = " + binding.tweenAnimationImg.scaleX + "," + binding.tweenAnimationImg.scaleY)
                }

                override fun onAnimationRepeat(animation: Animation?) {
                }

            })

            binding.tweenAnimationImg.startAnimation(scaleAnimation)
        }
        binding.translateBtn.setOnClickListener {
            val translateAnimation = TranslateAnimation(0f, 200f, 0f, 0f)
            translateAnimation.interpolator = LinearInterpolator()
            translateAnimation.duration = 800
            translateAnimation.fillAfter = true
            binding.tweenAnimationImg.startAnimation(translateAnimation)
        }
        binding.testOutIn.setOnClickListener {
            // 淡入动画
            binding.targetView.apply {
                alpha = 0f
                visibility = View.VISIBLE
                animate().alpha(1f).setDuration(600).setListener(null)
            }

            // 淡出动画
            binding.pb.apply {
                alpha = 1.0f
                animate().alpha(0f).setDuration(600).setListener(object : AnimatorListenerAdapter() {
                    override fun onAnimationEnd(animation: Animator?) {
                        binding.pb.visibility = View.GONE
                    }
                })
            }

        }
        binding.backgroundText.rotationY = 90f
        binding.testRotation.setOnClickListener {
            val visibleView: TextView
            val goneView: TextView

            if (binding.frontText.visibility == View.GONE) {
                visibleView = binding.backgroundText
                goneView = binding.frontText
            } else {
                visibleView = binding.frontText
                goneView = binding.backgroundText
            }

            val visible2Gone = ObjectAnimator.ofFloat(visibleView, "rotationY", 0f, -90f)
            visible2Gone.duration = 500
            visible2Gone.interpolator = LinearInterpolator()// 动画开始速率慢，后面加速

            val gone2Visible = ObjectAnimator.ofFloat(goneView, "rotationY", 90f, 0f)
            gone2Visible.duration = 500
            gone2Visible.interpolator = LinearInterpolator() // 动画开始速快，后面减速

            visible2Gone.addListener(object : AnimatorListenerAdapter() {
                override fun onAnimationEnd(animation: Animator?) {
                    visibleView.visibility = View.GONE
                    gone2Visible.start()
                    goneView.visibility = View.VISIBLE
                }
            })

            visible2Gone.start()
        }
    }


    private fun toast(msg: String) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
    }
}


